<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/2/1
 * Time: 上午1:06
 */

namespace Home\Model;
use Think\Model;

class MessageModel extends Model {

    protected $tableName = 'message';
    protected $pk = 'id';

    //获取消息首页－系统消息
    public function getMessageSystem($user_id){
        $message_field = array('id','content','time','is_read');
        $message_order = '`time` desc';
        $message_map['publisher_id'] = array('eq',0);
        $message_map['receiver_id'] = array(array('eq',0),array('eq',$user_id),'or');
        $message_result = $this->field($message_field)->order($message_order)->where($message_map)->select();
        if(is_array($message_result)){
            return $message_result;
        }else{
            return false;
        }
    }

    //获取消息首页－用户消息
    public function getMessageUser($user_id){
        //获取该登陆者的所有相关消息, 包括发布的和接收的
        $message_field = array('max(id)','publisher_id','receiver_id');
        $message_order = '`time` desc';
        $message_map[0]['receiver_id'] = array('eq',$user_id);
        $message_map[0]['publisher_id'] = array('neq',0);
        $message_map[0]['_logic'] = 'and';
        $message_map[1]['receiver_id'] = array('neq',0);
        $message_map[1]['publisher_id'] = array('eq',$user_id);
        $message_map[1]['_logic'] = 'and';
        $message_map['_logic'] = 'or';
        $message_result = $this->field($message_field)->where($message_map)->order($message_order)->group('publisher_id,receiver_id')->select();
        //dump($message_result);

        //冒泡排序－根据时间排序
        for($order1 = 0 ; $order1 < count($message_result) ; $order1++){
            for( $order2 = 0 ; $order2 < count($message_result) - $order1 ; $order2++ ){
                if($message_result[$order2]['max(id)'] < $message_result[$order2+1]['max(id)']){
                    $orderChange = $message_result[$order2];
                    $message_result[$order2] = $message_result[$order2+1];
                    $message_result[$order2+1] = $orderChange;
                }
            }
        }
        //dump($message_result);

        //****************************************************************************************************
        //消除对话之一
        $length = count($message_result);
        for( $i = 1 ; $i < $length ; $i++ ){
            for( $j = 0 ; $j < $i ; $j++ ){
                //发现重复
                if($message_result[$j]['publisher_id'] == $message_result[$i]['receiver_id'] && $message_result[$j]['receiver_id'] == $message_result[$i]['publisher_id'] )
                {
                    for( $k = $i ; $k < $length - 1 ; $k++ ){
                        $message_result[$k] = $message_result[$k+1];
                    }
                    $message_result[$length - 1] = NUll;
                    $length -= 1;
                    $i = $i - 1;
                    break;
                }
            }
        }

        //dump($message_result);
        //排除非空
        for( $m = count($message_result) ; $m >= 0  ; $m-- ){
            if( NULL == $message_result[$m] ){
                unset($message_result[$m]);
            }
        }
        //dump($message_result);
        if(is_array($message_result)){
            $result = array();
            $signal = 0;
            foreach($message_result as &$message_result_one){
                //message表获取信息
                $message_field_info = array('id','publisher_id','receiver_id','content','time','is_read');
                $message_map_info['id'] = array('eq',$message_result_one['max(id)']);
                $message_result_info = $this->field($message_field_info)->where($message_map_info)->select();
                if(is_array($message_result_info)){
                    $result[$signal] = $message_result_info[0];
                }
                if( $_SESSION['loginUser']['user_id'] == $message_result_info[0]['publisher_id'] ){
                        $result[$signal]['is_read'] = 2;
                }
                $content_arr = explode("!@#$%^&*",$message_result_info[0]['content']);
                $result[$signal]['content'] = $content_arr[0];


                //置换发布者和接收者ID
                if($_SESSION['loginUser']['user_id'] == $result[$signal]['publisher_id'] ){
                    $change = $result[$signal]['publisher_id'];
                    $result[$signal]['publisher_id'] = $result[$signal]['receiver_id'];
                    $result[$signal]['receiver_id'] = $change ;
                }

                //user表获取信息
                $user_field = array('user_name','image_url');
                $user_map['user_id'] = array('eq',$result[$signal]['publisher_id']);
                $user_db = M('User');
                $user_result = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_result)){
                    $result[$signal]['user_name'] = $user_result[0]['user_name'];
                    $result[$signal]['image_url'] = $user_result[0]['image_url'];
                }else{
                    //获取user表信息失败
                }

                $signal += 1 ;
            }
            //dump($message_result);
            return $result;
        }else{
            return false;
        }

    }

    //收发消息获取
    public function getMessageInfo($publisher_id,$receiver_id){
        $message_field = array('publisher_id','receiver_id','content','time','is_read');
        $message_order = '`time` asc';
        $message_map[0]['publisher_id'] = array('eq',$publisher_id);
        $message_map[0]['receiver_id'] = array('eq',$receiver_id);
        $message_map[0]['_logic'] = 'and';
        $message_map[1]['publisher_id'] = array('eq',$receiver_id);
        $message_map[1]['receiver_id'] = array('eq',$publisher_id);
        $message_map[1]['_logic'] = 'and';
        $message_map['_logic'] = 'or';
        //dump($message_map);
        $message_result = $this->field($message_field)->order($message_order)->where($message_map)->select();
        //dump($message_result);
        if(is_array($message_result)){
            foreach($message_result as &$message_result_one){
                if($message_result_one['receiver_id'] == $receiver_id){
                    $message_result_one['is_self'] = 1;
                }else{
                    $message_result_one['is_self'] = 2;
                }


                //增加用户名和头像
                $user_db = M('User');
                $user_field = array('user_name','image_url');
                $user_publisher_map['user_id'] = array('eq',$message_result_one['publisher_id']);
                $user_publisher = $user_db->field($user_field)->where($user_publisher_map)->select();
                if(is_array($user_publisher)){
                    $message_result_one['publisher_name'] = $user_publisher[0]['user_name'];
                    $message_result_one['publisher_image_url'] = $user_publisher[0]['image_url'];
                }else{
                    //获取USER表信息失败
                }

                $user_receiver_map['user_id'] = array('eq',$message_result_one['receiver_id']);
                $user_receiver = $user_db->field($user_field)->where($user_receiver_map)->select();
                if(is_array($user_receiver)){
                    $message_result_one['receiver_name'] = $user_receiver[0]['user_name'];
                    $message_result_one['receiver_image_url'] = $user_receiver[0]['image_url'];
                }else{
                    //获取USER表信息失败
                }

                //适应前端
                 $message_result_one['image_url'] = $user_publisher[0]['image_url'];


                //修改之添加项目名称及链接
                $content_arr = explode("!@#$%^&*",$message_result_one['content']);
                $message_result_one['content'] = $content_arr[0];
                $message_result_one['mission_name'] = $content_arr[1];
                $message_result_one['url'] = $content_arr[2];


            }
            return $message_result;
        }else{
            $message_null = array();
            return $message_null;
        }
    }


    public function getIsRead($receiver_id){
        $message_map['receiver_id'] = array('eq',$receiver_id);
        $message_map['is_read'] = array('eq',1);
        $message_result = $this->where($message_map)->select();
        if(is_array($message_result)){
            return $message_result;
        }else{
            return false;

        }
    }

    //获取有新的申请者的用户的手机号码
    public function getPhones(){
        $message_field = array('id','receiver_id','time');
        $message_map['is_read'] = array('eq',1);
        $message_map['content'] = array(array('like','%!@#$%'),array('notlike','你好，你已通过审核！!@#$%'),'and');
        $message_result = $this->where($message_map)->group('receiver_id')->field($message_field)->select();
        //dump($message_result);
        if(is_array($message_result)){
            foreach($message_result as &$message_result_one){
                $user_db = M('User');
                $user_info = $user_db->getByUser_id($message_result_one['receiver_id']);
                if(is_array($user_info)){
                    $message_result_one['phone'] = $user_info['phone'];
                }else{
                    //
                }
            }
            return $message_result;

        }else{
            return false;
        }
    }


    //获取有未读消息的用户的手机号码
    public function getNoReadMessagePhone(){
        $message_field = array('id','receiver_id');
        $message_map['is_read'] = array('eq',1);
        $message_map['content'] = array(array('like','你好，你已通过审核！!@#$%'),array('notlike','%!@#$%'),'or');
        $message_result = $this->where($message_map)->group('receiver_id')->field($message_field)->select();
        //dump($message_result);
        if(is_array($message_result)){
            foreach($message_result as &$message_result_one){
                $user_db = M('User');
                $user_info = $user_db->getByUser_id($message_result_one['receiver_id']);
                if(is_array($user_info)){
                    $message_result_one['phone'] = $user_info['phone'];
                }else{
                    //
                }
            }
            return $message_result;

        }else{
            return false;
        }
    }

} 